<?php

class ActivityDailyLoginService extends AbstractService
{
    /**
     * @var ActivityDailyLoginService
     */
    private static $instance;

    /**
     * Get the instance of ActivityDailyLoginService.
     *
     * @return ActivityDailyLoginService
     */
    public static function get()
    {
        if (!self::$instance) {
            self::$instance = new ActivityDailyLoginService();
        }
        return self::$instance;
    }

    /**
     * Construction.
     */
    protected function __construct()
    {
        parent::__construct();
    }

    /**
     * 处理用户创建时的逻辑
     *
     * @param int $shardId
     * @return void
     */
    public function handleCreate($shardId)
    {

    }

    /**
     * 处理用户登录时的逻辑
     *
     * @param int $userId
     * @return void
     */
    public function handleLogin($userId)
    {
        // do nothing
    }

    /**
     * 处理 service 对外提供的全量数据
     *
     * @param int $userId
     * @return array
     */
    public function info($userId)
    {
        $dailyLoginList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_DAILY_LOGIN);
        return [
            $dailyLoginList
        ];
    }

    /**
     * 刷新每日登录活动
     * @param int $userId
     * @param int $indexId
     * @param int $endTime
     * @param int $reqTime
     * @return SzAbstractVoList
     * @throws SzException
     */
    public function flush($userId, $indexId, $endTime, $reqTime)
    {
        /**
         * @var ActivityDailyLoginVo $dailyLogin
         */
        $dailyLoginList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_DAILY_LOGIN);
        $dailyLoginSettings = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_DAILY_LOGIN, null, true);

        if (!isset($dailyLoginSettings[$indexId]) || $endTime < $reqTime) {
            $dailyLoginList->deleteElement($indexId, false);
            SzPersister::get()->setVoList($dailyLoginList);
        } else if (!$dailyLoginList->getElement($indexId, false)) {
            $dailyLogin = new ActivityDailyLoginVo($userId, $indexId, 0, 0, 1,true);
            $dailyLoginList->addElement($dailyLogin);
            $dailyLoginList->setElement($indexId, $dailyLogin);
            SzPersister::get()->setVoList($dailyLoginList);
        }
        return $dailyLoginList;
    }

    /**
     * 每日登陆活动领奖
     * @param $userId
     * @param $indexId
     * @param $reqTime
     * @return bool
     * @throws SzException
     */
    public function getLoginRewards($userId, $indexId, $reqTime)
    {
        $dailyLoginSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_DAILY_LOGIN, $indexId, true);
        /**
         * @var ActivityDailyLoginVo $dailyLogin
         */
        $dailyLoginList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_DAILY_LOGIN);
        $dailyLogin = $dailyLoginList->getElement($indexId);
        $statusChange = false;
        $status = $dailyLogin->getStatus();
        $lastTime = $dailyLogin->getLastTime();
        if ($lastTime && ($reqTime < $lastTime || SzTime::getDate($lastTime) == SzTime::getDate($reqTime))) {//今天已经领取过奖励
            throw new SzException(22103, [$indexId, SzTime::getDate($lastTime), SzTime::getDate($reqTime)]);
        }
        if ($lastTime && isset($dailyLoginSetting['award'][$dailyLogin->getDay()])) {
            $day = $dailyLogin->getDay() + 1;
        } else {
            $day = 1;
            if ($status && $dailyLogin->getDay()) {
                $statusChange = true;
            }
        }

        if ($status && !$statusChange) {
            $reward = $dailyLoginSetting['award1'][$day - 1];
        } else {
            $reward = $dailyLoginSetting['award'][$day - 1];
        }

        $tid = '';

        foreach ($reward as $entityId => $count) {
            GameService::get()->handleEntity($userId, $entityId, $count, true);
            $tid .= $entityId . ':' . $count . '|';
        }
        CommonFunctionService::get()->reportResourceUseDetails(99999999,$userId,1,$tid,'getLoginRewards');//获得资源上报


        $dailyLogin->setDay($day);
        $dailyLogin->setLastTime($reqTime);
        if ($statusChange) {
            $dailyLogin->setStatus(0);
        }
        $dailyLoginList->updateElement($indexId, $dailyLogin);
        SzPersister::get()->setVoList($dailyLoginList);


        // 数据采集:七日签到
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:sign|action:sign|',
                'item' => $day,
                'value' => 1
            )
        ));

        // 数据采集:七日签到产出
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_RESOURCE_OUTPUT, array(
            'clientData' => array(
                'tid' => $tid,
                'item' => 'sign_sign&' . $day
            )
        ));
        return true;
    }
}